
Imports std
Imports System.IO
Imports common

Module main

    Public Class typeDefinition
        Public Const number As Char = character.n
        Public Const [string] As Char = character.s
    End Class

    Public Class struc
        Public index As Int32
        Public type As Char
        Public value As Object

        Public Sub New()
        End Sub

        Public Sub New(ByVal i As Int32, ByVal t As Char, ByRef v As String)
            index = i
            type = t
            If charsame(t, typeDefinition.number) Then
                copy(Of Double)(value, Convert.ToDouble(v))
            Else
                copy(Of String)(value, v)
            End If
        End Sub
    End Class

    Sub Main(ByVal args() As String)
        On Error Resume Next
        Dim reader As TextReader = Nothing
        Dim writer As TextWriter = Nothing
        Dim input As String = Nothing
        Dim increaseItem As vector(Of struc) = Nothing
        Dim parts As vector(Of String) = Nothing
        Dim i As Int32

        If args.Length < 2 Or args.Length Mod 3 <> 2 Then
            raiseError("argument count error, increase [columnIndex0 type0 value0 " + _
                        "columnIndex1 type1 value1 ... ] input output, " + _
                        "type can be ns, backslash supported" + _
                        ", if input or output is /-, use console.", errorHandle.errorType.critical)
        Else
            increaseItem = New vector(Of struc)
            For i = 0 To args.Length() - 5 Step 3
                unescape(args(i + 2))
                increaseItem.push_back(New struc(Convert.ToInt32(args(i)), args(i + 1)(0), args(i + 2)))
            Next

            If args(args.Length() - 2) = "/-" Then
                reader = Console.In
            Else
                reader = New StreamReader(New FileStream(args(args.Length() - 2), _
                                                FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            End If
            assert(Not reader Is Nothing, "file no found " + args(args.Length() - 2))

            If args(args.Length() - 1) = "/-" Then
                writer = Console.Out
            Else
                writer = New StreamWriter(New FileStream(args(args.Length() - 1), _
                                                FileMode.Create, FileAccess.Write, FileShare.Read))
            End If
            assert(Not writer Is Nothing, "file no found " + args(args.Length() - 1))

            input = reader.ReadLine
            While Not input Is Nothing
                strsepar(input, character.tab, parts)
                For i = 0 To increaseItem.size() - 1
                    Dim index As Int32 = increaseItem(i).index
                    Dim type As Char = increaseItem(i).type
                    Dim value As Object = increaseItem(i).value
                    If index < parts.size() Then
                        If type = typeDefinition.number Then
                            parts(index) = Convert.ToString(Convert.ToDouble(parts(index)) + value)
                        Else
                            parts(index) += value
                        End If
                    End If
                Next

                For i = 0 To parts.size() - 2
                    writer.Write(parts(i) + character.tab)
                Next
                writer.WriteLine(parts(i))

                input = reader.ReadLine
            End While

            closestream(reader)
            closestream(writer)
        End If
    End Sub

End Module
